package com.qf.web;

import com.qf.pojo.Emp;
import com.qf.service.EmpService;
import com.qf.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/deleteEmp")
public class DeleteEmpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        EmpService empService=new EmpServiceImpl();
        PrintWriter writer = resp.getWriter();
        String ename = req.getParameter("ename");
        if ("".equals(ename) || ename == null) {
            writer.println("请输入要删除的员工名称");
        } else {
            //查询该员工是否存在
            List<Emp> empByEname = empService.findEmpByEname(ename);
            if(empByEname.size()==0){
                writer.println("不存在该员工信息");
            }else {
                int num = empService.deleteEmpByEname(ename);
                if(num==1){
                    resp.sendRedirect("/findEmpAll");
                }else {
                    writer.println("员工信息删除失败");
                }
            }


        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
